﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HotelManageSystem.Domain;
using HotelManageSystem.Domain.Entities;
using HotelManageSystem.Domain.Infrastructure;
using HotelManageSystem.Models;
using Microsoft.Reporting.WebForms;
using Telerik.Web.Mvc;

namespace HotelManageSystem.Controllers
{
    public class AttendanceController : Controller
    {
        private IEmployeeRepository employeeRepository;
        private IUnitOfWork unitOfWork;
        private IAttendanceRepository attendanceRepository;
        public AttendanceController(IEmployeeRepository employeeRepository,
                         IUnitOfWork unitOfWork, IAttendanceRepository attendanceRepository)
        {
            this.employeeRepository = employeeRepository;
            this.unitOfWork = unitOfWork;
            this.attendanceRepository = attendanceRepository;
        }

        public IList<AttendanceModel> EmployeeToAttendanceModel()
        {
            //给员工添加考勤数据的不会是已经离职的员工
            var employees = employeeRepository.GetMany(e => e.EmployeeStatus != "离职状态");
            IList<AttendanceModel> attendanceModels = new List<AttendanceModel>();
            foreach (Employee e in employees)
            {
                attendanceModels.Add(new AttendanceModel()
                                         {
                                             EmployeeId = e.ID,
                                             Name = e.Name,
                                             Department = e.Department.Name,
                                             Post = e.Post.Title
                                         });
            }
            return attendanceModels;
        }
        public PartialViewResult InsertIndex()
        {
            return PartialView(EmployeeToAttendanceModel());
        }
        //添加考勤数据的Index页面的ajax
        [GridAction]
        public ActionResult InsertAttendanceAjax()
        {
            return View(new GridModel(EmployeeToAttendanceModel()));
        }
        //添加考勤记录的HttpGet页面
        [HttpGet]
        public PartialViewResult Insert(int id)
        {
            Employee employee = employeeRepository.GetById(id);
            AttendanceModel attendanceModel;
            if (employee != null)
            {
                attendanceModel = new AttendanceModel()
                                      {
                                          EmployeeId = employee.ID,
                                          Name = employee.Name,
                                          AttendDate = DateTime.Now,
                                          StartTime = DateTime.Now.AddHours(-10),
                                          EndTime = DateTime.Now.AddHours(-1)
                                      };
            }
            else
            {
                attendanceModel = new AttendanceModel();
            }
            return PartialView(attendanceModel);
        }
        //根据用户名和和考勤时间判断是否已经添加了当天该员工的考勤数据
        public JsonResult CheckIn(string id, string attendDate)
        {
            int employeeId = Convert.ToInt32(id);
            DateTime time = Convert.ToDateTime(attendDate);
            var attendanceModel =
                attendanceRepository.GetMany(
                    a => a.EmployeeId == employeeId & a.AttendDate.ToShortDateString() == time.ToShortDateString());

            var b = attendanceModel.Count() == 0 ? "true" : "false";

            JsonResult json = new JsonResult()
                                  {
                                      Data = new { Value = b }
                                  };
            return Json(json);
        }

        //添加考勤记录HttpPost页面
        [HttpPost]
        public RedirectToRouteResult Insert(AttendanceModel attendanceModel)
        {
            if (ModelState.IsValid)
            {
                Attendance attendance = new Attendance()
                 {
                     EmployeeId = attendanceModel.EmployeeId,
                     AttendDate = attendanceModel.AttendDate,
                     StartTime = attendanceModel.StartTime,
                     EndTime = attendanceModel.EndTime,
                     AttendaceStatus = attendanceModel.AttendaceStatus,
                     AttendaceRemark = attendanceModel.AttendaceRemark
                 };
                attendanceRepository.Add(attendance);
                unitOfWork.Commit();
            }
            Session["url"] = "/Attendance/InsertIndex";
            return RedirectToAction("Index", "HumanResources");
        }

        //将Attendance转换为AttendanceModel,循环所有的考勤记录数据
        public IList<AttendanceModel> AttendanceToAttendanceModel()
        {
            var attendances = attendanceRepository.GetAll();
            IList<AttendanceModel> attendanceModels = new List<AttendanceModel>();
            Employee employee;
            foreach (Attendance attendance in attendances)
            {
                employee = employeeRepository.GetById(attendance.EmployeeId);
                attendanceModels.Add(new AttendanceModel()
                                         {
                                             Id = attendance.ID,
                                             EmployeeId = attendance.EmployeeId,
                                             Name = employee.Name,
                                             EmployeeStatus = employee.EmployeeStatus,
                                             AttendDate = attendance.AttendDate,
                                             StartTime = attendance.StartTime,
                                             EndTime = attendance.EndTime,
                                             AttendaceStatus = attendance.AttendaceStatus,
                                             AttendaceRemark = attendance.AttendaceRemark,
                                         });
            }
            return attendanceModels;
        }

        //管理考勤数据的起始页面
        public PartialViewResult ManageIndex()
        {
            return PartialView(AttendanceToAttendanceModel());
        }
        //管理考勤数据,Telerik的分页及刷新
        [GridAction]
        public ActionResult ManageAttendanceAjax()
        {
            return View(new GridModel(AttendanceToAttendanceModel()));
        }

        //删除考勤记录
        public PartialViewResult DeleteAttendance(int id)
        {
            Attendance attendance = attendanceRepository.GetById(id);
            if (attendance != null)
            {
                attendanceRepository.Delete(attendance);
                unitOfWork.Commit();
            }
            return PartialView("ManageIndex", AttendanceToAttendanceModel());
        }

        //修改考勤数据的HttpGet页面
        [HttpGet]
        public PartialViewResult EditAttendance(int id)
        {
            Attendance attendance = attendanceRepository.GetById(id);
            AttendanceModel attendanceModel;
            if (attendance != null)
            {
                attendanceModel = new AttendanceModel()
                                      {
                                          Id = id,
                                          Name = attendance.Employee.Name,
                                          EmployeeId = attendance.EmployeeId,
                                          AttendDate = attendance.AttendDate,
                                          StartTime = attendance.StartTime,
                                          EndTime = attendance.EndTime,
                                          AttendaceStatus = attendance.AttendaceStatus,
                                          AttendaceRemark = attendance.AttendaceRemark
                                      };
            }
            else
            {
                attendanceModel = new AttendanceModel();
            }
            return PartialView(attendanceModel);
        }
        //修改考勤数据的HttpPost页面
        [HttpPost]
        public RedirectToRouteResult EditAttendance(AttendanceModel attendanceModel)
        {
            Attendance attendance = attendanceRepository.GetById(attendanceModel.Id);
            if (attendance != null)
            {
                attendance.AttendDate = attendanceModel.AttendDate;
                attendance.StartTime = attendanceModel.StartTime;
                attendance.EndTime = attendanceModel.EndTime;
                attendance.AttendaceStatus = attendanceModel.AttendaceStatus;
                attendance.AttendaceRemark = attendanceModel.AttendaceRemark;
                attendanceRepository.Update(attendance);
                unitOfWork.Commit();
            }
            Session["url"] = "/Attendance/ManageIndex";
            return RedirectToAction("Index", "HumanResources");
        }
    }
}
